package io.github.bigbird0101.pipeline;

import java.util.function.Predicate;

public abstract class AbstractPredicateEventHandler extends AbstractPredicateObjectHandler<HandlerEvent> implements EventHandler{
    public AbstractPredicateEventHandler(Predicate<HandlerEvent>... predicates) {
        super(predicates);
    }

    @Override
    public void handle(HandlerContext context, HandlerEvent handlerEvent) {
        for(Predicate<HandlerEvent> predicate:getPredicates()){
            if(!predicate.test(handlerEvent)){
                return;
            }
        }
        if(!(context instanceof EventHandlerContext)){
            return;
        }
        HandlerEvent doHandleEvent=doHandle(context,handlerEvent);
        if(null!=doHandleEvent) {
            ((EventHandlerContext) context).sendEvent(doHandleEvent);
        }
    }
}
